package com.jeequan.jeepay.service.impl;

//package .service.impl;
import cn.hutool.core.util.StrUtil;
import com.jeequan.jeepay.core.entity.PayChannel;
import com.jeequan.jeepay.service.PayChannelService;
import com.jeequan.jeepay.service.mapper.PayChannelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
//import .entity.PayChannel;
//import .mapper.PayChannelMapper;
//import .service.PayChannelService;
 /**
 * 供应商列表;(pay_channel)表服务实现类
 * @author : wondream322@gmail.com
 * @date : 2023-8-19
 */
@Service
public class PayChannelServiceImpl implements PayChannelService {
    @Autowired
    private PayChannelMapper payChannelMapper;
    
    /** 
     * 通过ID查询单条数据 
     *
     * @param id 主键
     * @return 实例对象
     */
    public PayChannel queryById(Integer id){
        return payChannelMapper.selectById(id);
    }
    
    /**
     * 分页查询
     *
     * @param payChannel 筛选条件
     * @param current 当前页码
     * @param size  每页大小
     * @return
     */
    public Page<PayChannel> paginQuery(PayChannel payChannel, long current, long size){
        //1. 构建动态查询条件
        LambdaQueryWrapper<PayChannel> queryWrapper = new LambdaQueryWrapper<>();
        if(StrUtil.isNotBlank(payChannel.getCode())){
            queryWrapper.eq(PayChannel::getCode, payChannel.getCode());
        }
        if(StrUtil.isNotBlank(payChannel.getTitle())){
            queryWrapper.eq(PayChannel::getTitle, payChannel.getTitle());
        }
        if(StrUtil.isNotBlank(payChannel.getMerchantNo())){
            queryWrapper.eq(PayChannel::getMerchantNo, payChannel.getMerchantNo());
        }
        if(StrUtil.isNotBlank(payChannel.getSignKey())){
            queryWrapper.eq(PayChannel::getSignKey, payChannel.getSignKey());
        }
        if(StrUtil.isNotBlank(payChannel.getPayGateway())){
            queryWrapper.eq(PayChannel::getPayGateway, payChannel.getPayGateway());
        }
        if(StrUtil.isNotBlank(payChannel.getPageReturnUrl())){
            queryWrapper.eq(PayChannel::getPageReturnUrl, payChannel.getPageReturnUrl());
        }
        if(StrUtil.isNotBlank(payChannel.getNotifyUrl())){
            queryWrapper.eq(PayChannel::getNotifyUrl, payChannel.getNotifyUrl());
        }
        if(StrUtil.isNotBlank(payChannel.getPublicKey())){
            queryWrapper.eq(PayChannel::getPublicKey, payChannel.getPublicKey());
        }
        if(StrUtil.isNotBlank(payChannel.getPrivateKey())){
            queryWrapper.eq(PayChannel::getPrivateKey, payChannel.getPrivateKey());
        }
        if(StrUtil.isNotBlank(payChannel.getPlatformPublicKey())){
            queryWrapper.eq(PayChannel::getPlatformPublicKey, payChannel.getPlatformPublicKey());
        }
        if(StrUtil.isNotBlank(payChannel.getQueryGateway())){
            queryWrapper.eq(PayChannel::getQueryGateway, payChannel.getQueryGateway());
        }
        //2. 执行分页查询
        Page<PayChannel> pagin = new Page<>(current , size , true);
        IPage<PayChannel> selectResult = payChannelMapper.selectByPage(pagin , queryWrapper);
        pagin.setPages(selectResult.getPages());
        pagin.setTotal(selectResult.getTotal());
        pagin.setRecords(selectResult.getRecords());
        //3. 返回结果
        return pagin;
    }
    
    /** 
     * 新增数据
     *
     * @param payChannel 实例对象
     * @return 实例对象
     */
    public PayChannel insert(PayChannel payChannel){
        payChannelMapper.insert(payChannel);
        return payChannel;
    }
    
    /** 
     * 更新数据
     *
     * @param payChannel 实例对象
     * @return 实例对象
     */
    public PayChannel update(PayChannel payChannel){
        //1. 根据条件动态更新
        LambdaUpdateChainWrapper<PayChannel> chainWrapper = new LambdaUpdateChainWrapper<PayChannel>(payChannelMapper);
        if(StrUtil.isNotBlank(payChannel.getCode())){
            chainWrapper.set(PayChannel::getCode, payChannel.getCode());
        }
        if(StrUtil.isNotBlank(payChannel.getTitle())){
            chainWrapper.set(PayChannel::getTitle, payChannel.getTitle());
        }
        if(StrUtil.isNotBlank(payChannel.getMerchantNo())){
            chainWrapper.set(PayChannel::getMerchantNo, payChannel.getMerchantNo());
        }
        if(StrUtil.isNotBlank(payChannel.getSignKey())){
            chainWrapper.set(PayChannel::getSignKey, payChannel.getSignKey());
        }
        if(StrUtil.isNotBlank(payChannel.getPayGateway())){
            chainWrapper.set(PayChannel::getPayGateway, payChannel.getPayGateway());
        }
        if(StrUtil.isNotBlank(payChannel.getPageReturnUrl())){
            chainWrapper.set(PayChannel::getPageReturnUrl, payChannel.getPageReturnUrl());
        }
        if(StrUtil.isNotBlank(payChannel.getNotifyUrl())){
            chainWrapper.set(PayChannel::getNotifyUrl, payChannel.getNotifyUrl());
        }
        if(StrUtil.isNotBlank(payChannel.getPublicKey())){
            chainWrapper.set(PayChannel::getPublicKey, payChannel.getPublicKey());
        }
        if(StrUtil.isNotBlank(payChannel.getPrivateKey())){
            chainWrapper.set(PayChannel::getPrivateKey, payChannel.getPrivateKey());
        }
        if(StrUtil.isNotBlank(payChannel.getPlatformPublicKey())){
            chainWrapper.set(PayChannel::getPlatformPublicKey, payChannel.getPlatformPublicKey());
        }
        if(StrUtil.isNotBlank(payChannel.getQueryGateway())){
            chainWrapper.set(PayChannel::getQueryGateway, payChannel.getQueryGateway());
        }
        //2. 设置主键，并更新
        chainWrapper.eq(PayChannel::getId, payChannel.getId());
        boolean ret = chainWrapper.update();
        //3. 更新成功了，查询最最对象返回
        if(ret){
            return queryById(payChannel.getId());
        }else{
            return payChannel;
        }
    }
    
    /** 
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    public boolean deleteById(Integer id){
        int total = payChannelMapper.deleteById(id);
        return total > 0;
    }
}